package com.sx.jys.ui.frag

import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.widget.CompoundButton
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.sanxinsaas.nearstore.framework.util.click
import com.sx.jys.app.framework.BaseFragment
import com.sx.jys.app.net.bean.SwitchEnum
import com.sx.jys.app.until.CacheUntil
import com.sx.jys.app.until.toRate
import com.sx.jys.app.until.toRateNoUntil
import com.sx.jys.databinding.FragSettingBinding
import com.sx.jys.ui.act.FxListAct
import com.sx.jys.ui.act.LoginAct
import com.sx.jys.ui.vm.SettingVm

class FragSetting : BaseFragment<SettingVm, FragSettingBinding>() {
    companion object {
        val instance = FragSetting()
    }

    val orderSwicthListener = object : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
            mViewModel.orderSwitch(p1)
        }
    }
    val zsSwicthListener = object : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
            mViewModel.zsSwitch(p1)
        }
    }



    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.orderSwitch.setOnCheckedChangeListener(orderSwicthListener)
        mDatabind.zsSwitch2.setOnCheckedChangeListener(zsSwicthListener)
        (mDatabind.tvTitle.layoutParams as ViewGroup.MarginLayoutParams).let {
            it.topMargin = ImmersionBar.getStatusBarHeight(this)
        }
        mDatabind.settingLogout.click {
            XPopup.Builder(requireContext())
                .asConfirm("提示", "是否退出登录") {
                    mViewModel.logout {
                        CacheUntil.setToken("")
                        startActivity(Intent(requireContext(), LoginAct::class.java))
                        activity?.finish()
                    }
                }.show()
        }
        mDatabind.settingBzjMax.click {
            //最大保证金
            XPopup.Builder(requireContext())
                .asInputConfirm("提示", "", "", "请输入最大可用保证金") {

                }.show()
        }
        mDatabind.settingBzj.click {
            //下单保证金
            XPopup.Builder(requireContext())
                .asInputConfirm("提示", "", "", "请输入下单保证金") {

                }.show()
        }
        mDatabind.settingFx.click {
            //风险控制
            startActivity(Intent(requireContext(), FxListAct::class.java))
        }
        mDatabind.settingSsRate.click {
            //扫损比例
            XPopup.Builder(requireContext())
                .asInputConfirm(
                    "提示",
                    "",
                    mViewModel.ssRatoLiveData.value?.vals.orEmpty(),
                    "请输入扫损比例"
                ) {
                    XPopup.Builder(requireContext())
                        .asConfirm("提示","请确认设置扫损比例为${it}%"){
                            mViewModel.reqSetting(SwitchEnum.SS_RATO.mValue,"${it.toRateNoUntil()}")
                        }.show()
                }.show()
        }
        mDatabind.settingSsTime.click {
            //扫损时间

            XPopup.Builder(requireContext())
                .asInputConfirm(
                    "扫损时间",
                    "",
                    mViewModel.ssTimeLiveData.value?.vals.orEmpty(),
                    "请输入扫损时间"
                ) {
                    XPopup.Builder(requireContext())
                        .asConfirm("提示","请确认设置扫损时间为${it}时"){
                            mViewModel.reqSetting(SwitchEnum.SS_TIME.mValue,"${it}")
                        }.show()
                }.show()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.vm = mViewModel
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.switchStatus.observe(this) {
            mDatabind.orderSwitch.setOnCheckedChangeListener(null)
            mDatabind.orderSwitch.isChecked = it
            mDatabind.orderSwitch.setOnCheckedChangeListener(orderSwicthListener)
        }
        mViewModel.switchZsStatus.observe(this){
            mDatabind.zsSwitch2.setOnCheckedChangeListener(null)
            mDatabind.zsSwitch2.isChecked = it
            mDatabind.zsSwitch2.setOnCheckedChangeListener(zsSwicthListener)
        }
        mViewModel.ssRatoLiveData.observe(this) {
            mDatabind.settingSsRate.setRightText("${it.vals.toRate()}")
        }
        mViewModel.ssTimeLiveData.observe(this) {
            mDatabind.settingSsTime.setRightText("${it.vals}小时")
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.reqOrderSwitch()
        mViewModel.showMoreUi.value=CacheUntil.showMoreFun()
    }
}